Store only what you need: the edges.

  • The simplest representation is an edge list, which stores only the edges as pairs (for undirected graphs) or ordered pairs (for directed graphs), with optional weights.
  • This format is minimal and very easy to read or write from a file, making it excellent for data interchange and representing sparse graphs where memory is a concern.
  • Its main limitation is performance: operations like checking if two nodes are adjacent require scanning the entire list of edges, which can be slow for large graphs.
Python: Edge List Examples
# Undirected, unweighted edge list
V = ["A", "B", "C", "D"]
E = {frozenset({"A","B"}), frozenset({"A","C"}), frozenset({"B","D"})}

# Directed, weighted edge list
Vd = ["A", "B", "C"]
Ed = {("A","B",3), ("B","C",2), ("C","A",5), ("A","A",1)}